package com.yanqu.road.tools;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelListener extends AnalysisEventListener {
    private Map<Integer, ExcelHead> headMap = new HashMap<>();

    private Map<Integer, ConfigDesc> descMap = new HashMap<>();

    private List<Map<Object,Object>> dataList = new ArrayList<>();


    @Override
    public void invokeHeadMap(Map headMap, AnalysisContext context) {
        for (Object key : headMap.keySet()) {
            Integer keyNum = (Integer) key;
            Object o = headMap.get(key);
            String str = (String) o;
            String[] arr = str.split("=");
            ExcelHead excelHead = new ExcelHead(arr[0], arr[1]);
            this.headMap.put(keyNum, excelHead);
        }
    }

    @Override
    public void invoke(Object data, AnalysisContext analysisContext) {
        int rowIndex = analysisContext.readRowHolder().getRowIndex();
        Map map = (Map) data;
        if (rowIndex == 1) {
           // 是描述啊
            for (Object key : map.keySet()) {
                Integer keyNum = (Integer) key;
                Object o = map.get(key);
                String str = (String) o;
                descMap.put(keyNum, new ConfigDesc(str));
            }
        }else {
            Map<Object, Object> dataMap = new HashMap<>();
            for (Object key : map.keySet()) {
                Object value = map.get(key);
                dataMap.put(key, value);
            }
            dataList.add(dataMap);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public ExcelData getExcelData() {
        return new ExcelData(headMap, descMap, dataList);
    }
}
